package com.kayang.wechat.controller;

import com.kayang.common.HttpMsg;
import com.kayang.wechat.config.WeChatConfig;
import com.kayang.wechat.service.AccessTokenService;
import com.kayang.wechat.pojo.DeptModel;
import com.kayang.wechat.pojo.WechatContext;
import com.kayang.wechat.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping(value = "/dept")
public class DeptController extends BaseController {
    @Autowired
    AccessTokenService tokenService;

    @Autowired
    WeChatConfig weChatConfig;

    @Autowired
    DeptService deptService;

    @PostMapping("/creat")
    public ResponseEntity creatDept(@Valid @RequestBody DeptModel deptModel){
        String url = weChatConfig.getPath() + "/cgi-bin/department/create?access_token=ACCESS_TOKEN";
        url = url.replace("ACCESS_TOKEN", tokenService.getABAccessTokenStr(weChatConfig.getAppKey()));
        WechatContext context = new WechatContext();
        context.setUrl(url);
        context.setMethod("POST");
        context.setBody(deptModel.toMap(deptModel));
        Map ret = deptService.creatDept(deptModel, context);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("创建成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }

    @PostMapping("/update")
    public ResponseEntity updateDept(@Valid @RequestBody DeptModel deptModel){
        String url = weChatConfig.getPath() + "/cgi-bin/department/update?access_token=ACCESS_TOKEN";
        url = url.replace("ACCESS_TOKEN", tokenService.getABAccessTokenStr(weChatConfig.getAppKey()));
        WechatContext context = new WechatContext();
        context.setUrl(url);
        context.setMethod("POST");
        context.setBody(deptModel.toMap(deptModel));
        Map ret = deptService.updateDept(deptModel, context);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("更新成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);

    }

    @GetMapping("/delete")
    public ResponseEntity deleteDept(@RequestParam int id){
        String url = weChatConfig.getPath() + "/cgi-bin/department/delete?access_token=ACCESS_TOKEN&id=ID";
        url = url.replace("ACCESS_TOKEN", tokenService.getABAccessTokenStr(weChatConfig.getAppKey()));
        url = url.replace("ID", Integer.toString(id));
        WechatContext context = new WechatContext();
        context.setUrl(url);
        context.setMethod("GET");
        Map ret = deptService.deleteDept(context);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("删除成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }

    @Deprecated
    @GetMapping("/list")
    public ResponseEntity getDeptList(@RequestParam int id){
        String url = weChatConfig.getPath() + "/cgi-bin/department/list?access_token=ACCESS_TOKEN&id=ID";
        url = url.replace("ACCESS_TOKEN", tokenService.getABAccessTokenStr(weChatConfig.getAppKey()));
        url = url.replace("ID", Integer.toString(id));
        WechatContext context = new WechatContext();
        context.setUrl(url);
        context.setMethod("GET");
        Map ret = deptService.getDeptList(context);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("获取成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }

    @Deprecated
    @GetMapping("/get")
    public ResponseEntity getDept(@RequestParam int id){
        String url = weChatConfig.getPath() + "/cgi-bin/department/get?access_token=ACCESS_TOKEN&id=ID";
        url = url.replace("ACCESS_TOKEN", tokenService.getABAccessTokenStr(weChatConfig.getAppKey()));
        url = url.replace("ID", Integer.toString(id));
        WechatContext context = new WechatContext();
        context.setUrl(url);
        context.setMethod("GET");
        Map ret = deptService.getDept(context);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("获取成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }

}